﻿using Autofac;
using Autofac.Integration.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using Zj.Blog.Domain.Interface;
using Zj.Blog.Repository;

namespace Zj.Blog.Mvc
{
    public class AutofacExt
    {
        public static IContainer _container;
        public static void Init()
        {
            var builder = new ContainerBuilder();

            /*注册基仓储和基单元工作模式*/
            builder.RegisterGeneric(typeof(BaseRepository<>)).As(typeof(IRepository<>));
            builder.RegisterType(typeof(UnitOfWork)).As(typeof(IUnitOfWork));

            /*注册仓储*/
            builder.RegisterAssemblyTypes(Assembly.GetAssembly(typeof(UserRepository))).AsImplementedInterfaces();

            /*注册控制器,使用属性注入*/
            builder.RegisterControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired();
            
            _container = builder.Build();

            DependencyResolver.SetResolver(new AutofacDependencyResolver(_container));
        }
    }
}